const { Controller } = require("egg");
const checkUserAgent = require("../util/checkUserAgent");

class SectionController extends Controller {
    //增加节
    async create() {
            try {
                const body = this.ctx.request.body
                body.orderby = Number(body.orderby)
                await this.ctx.service.section.addSection(body)
                this.ctx.body = {
                    code: 20000,
                    message: true
                }

            } catch (error) {
                this.ctx.body = {
                    code: 50000,
                    message: false
                }
            }
        }
        //查看节
    async index() {
            try {
                const chapter_id = this.ctx.request.query.chapter_id
                const sectionList = await this.ctx.service.section.getSection(chapter_id)
                this.ctx.body = {
                    code: 20000,
                    message: true,
                    data: sectionList
                }
            } catch (error) {
                this.ctx.body = {
                    code: 50000,
                    message: false,
                }
            }
        }
        //删除节
    async destroy() {
            try {
                const id = this.ctx.params.id
                await this.ctx.service.section.deleteSection(id)
                this.ctx.body = {
                    code: 20000,
                    message: true
                }
            } catch (error) {
                this.ctx.body = {
                    code: 50000,
                    message: false
                }
            }
        }
        //更新节
    async update() {
        try {
            const id = this.ctx.params.id
            const body = this.ctx.request.body
            await this.ctx.service.section.updateSection(id, body)
            this.ctx.body = {
                code: 20000,
                message: true
            }
        } catch (error) {
            this.ctx.body = {
                code: 50000,
                message: false
            }
        }
    }
    //查看本节内容
    async getSectionDetail(){
        const {ctx} = this;
        const section_id = ctx.params.id
        const ua = checkUserAgent(ctx.header['user-agent'])
        const sectionDetail = await ctx.service.website.getSectionPage(section_id)
        if(ua){
            await ctx.render("pc/sectionDetail.html",sectionDetail)
        }else{
            await ctx.render("mobile/sectionDetail.html",sectionDetail)
        }
        // this.ctx.body = sectionDetail.menuList
}
}

module.exports = SectionController